package com.caishi.lkx.exam.records;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.exam.records.dto.*;
import com.caishi.lkx.exam.records.vo.CategoryCollectRecordsDtoItemVo;
import com.caishi.lkx.exam.records.vo.NewRecordsDtoItemVo;

import java.util.function.Function;
import java.util.stream.Stream;

/**
 * 做题记录获取接口
 */
public interface RecordsGetDataService {
    /**
     * 获取科目做题数统计
     *
     * @param industryCode 行业code
     * @param examId       考试id
     * @param categoryId   科目id
     * @return 做题记录数量 错题记录数量 收藏记录数量
     */
    RecordsCountRecordDto getCategoryCountRecordsResult(String userId, String industryCode, String examId, Long categoryId);


    /**
     * 获取章节练习章节的做题记录
     *
     * @param industryCode 行业code
     * @param examId       考试id
     * @param categoryId   科目id
     * @param chapterId    章节id
     * @return 记录
     */
    QuestionRecordsDto getQuestionRecordsResult(String userId, String industryCode, String examId, Long categoryId, RecordsCollectType type, String id);


    /**
     * 获取章节做题数统计
     *
     * @param industryCode 行业code
     * @param examId       考试id
     * @param categoryId   科目id
     * @param chapterId    章节id
     * @return 做题记录数量 错题记录数量 收藏记录数量
     */
    RecordsCountRecordDto getCollectCountRecordsResult(String userId, String industryCode, String examId, Long categoryId, RecordsCollectType type, String id);

    /**
     * 获取科目下最新错题记录
     *
     * @param industryCode 行业code
     * @param examId       考试id
     * @param categoryId   科目id
     * @param size         获取大小
     * @return 记录
     */
    Page<NewRecordsDtoItemVo> getErrorRecordsResult(String userId, String industryCode, String examId, Long categoryId, int page, int size);

    NewQuestionRecordsDto getErrorRecordsResult(String userId, String industryCode, String examId, Long categoryId);

    /**
     * 获取科目下最新收藏记录
     *
     * @param industryCode 行业code
     * @param examId       考试id
     * @param categoryId   科目id
     * @param size         获取大小
     * @return 记录
     */
    Page<NewRecordsDtoItemVo> getCollectRecordsResult(String userId, String industryCode, String examId, Long categoryId, int page, int size);

    NewQuestionRecordsDto getCollectRecordsResult(String userId, String industryCode, String examId, Long categoryId);


    /**
     * 获取集合错题集试题数据
     *
     * @param userId
     * @param industryCode
     * @param examId
     * @param categoryId
     * @param type
     * @param id
     * @return
     */
    CollectionErrorAndCollectRecordDataDto getErrorInCollectDataResult(String userId, String industryCode, String examId, Long categoryId, RecordsCollectType type, String id);

    /**
     * 获取集合下收藏数据
     *
     * @param industryCode 行业code
     * @param examId       考试id
     * @param categoryId   科目id
     * @param chapterId    章节id
     * @return 记录
     */
    CollectionErrorAndCollectRecordDataDto getCollectInCollectDataResult(String userId, String industryCode, String examId, Long categoryId, RecordsCollectType type, String id);


    /**
     * 用户科目下做题集合记录
     *
     * @param userId
     * @param industryCode
     * @param examId
     * @param categoryId
     * @return
     */
    Page<CategoryCollectRecordsDtoItemVo> getCategoryCollectRecordsResult(String userId, String industryCode, String examId, Long categoryId, int page, int size);


    CategoryCollectRecordsDto getCategoryCollectRecordsResult(String userId, String industryCode, String examId, Long categoryId);

    /**
     * 获取栏目下视频观看记录
     * @param userId 用户id
     * @param industryCode 行业code
     * @param examId 考试id
     * @param categoryId 科目id
     * @param columnId 栏目id
     * @param chapterId 章节id
     * @return
     */
    VideoRecordsDto getVideoRecordsData(String userId, String industryCode, String examId, Long categoryId,String columnId,String chapterId);

    /**
     * 获取章节视频观看记录统计
     * @param userId 用户id
     * @param industryCode 行业code
     * @param examId 考试id
     * @param categoryId 科目id
     * @param columnId 栏目id
     * @param chapterId 章节id
     * @return
     */
    CountVideoRecordsDto getCountVideoRecordsData(String userId, String industryCode, String examId, Long categoryId,String columnId,String chapterId);

    /**
     * 获取栏目下视频观看记录统计
     * @param userId 用户id
     * @param industryCode 行业code
     * @param examId 考试id
     * @param categoryId 科目id
     * @param columnId 栏目id
     * @return
     */
    CountVideoRecordsDto getCountColumnVideoRecordsData(String userId, String industryCode, String examId, Long categoryId,String columnId);

    default <I, T extends OssMapDto<I, T>> Page<NewRecordsDtoItemVo> ossMapDtoPage(OssMapDto<I, T> dto, int page, int size) {
        return ossMapDtoPage(
                dto,
                data -> data.keySet().stream()
                        .map(key -> {
                            var vo = data.getObject(key, NewRecordsDtoItemVo.class);
                            vo.setQid(key);
                            return vo;
                        })
                        .sorted((v, v1) -> Long.compare(v1.getT(), v.getT()))
                , page, size);
    }

    default <I, T extends OssMapDto<I, T>, P> Page<P> ossMapDtoPage(OssMapDto<I, T> dto, Function<JSONObject, Stream<P>> flatMap, int page, int size) {
        var data = dto.getData();
        var result = new Page<P>(page, size);
        var flatData = flatMap.apply(data).toList();
        result.setTotal(flatData.size());
        result.setRecords(flatData.subList((page - 1) * size, Math.min(page * size, flatData.size())));
        data.clear();
        return result;
    }
}
